The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
Annotations in Java are a form of metadata that can be added to Java code (classes, methods, variables, etc.). They provide information to the compiler and can be used at runtime by the JVM or other tools for various purposes.
Types of Annotations
Standard Annotations: Java provides several built-in annotations in the
java.lang package.
@Override: Indicates that a method is overriding a method in a superclass.
@Deprecated: Marks a method or class as deprecated, meaning it should not be used.
@SuppressWarnings: Tells the compiler to suppress specific warnings.
public class MyClass {
@Override
public String toString() {
return "MyClass";
}
@Deprecated
public void oldMethod() {
// Do something
}
@SuppressWarnings("unchecked")
public void myMethod() {
// Do something
}
}
Meta-Annotations: Meta-annotations are annotations that apply to other annotations. Some of the meta-annotations are:
@Retention: Specifies how long the annotation is retained (runtime, class file, source).
@Target: Specifies the kinds of elements an annotation type applies to (methods, fields, classes).
@Documented: Indicates that an annotation should be documented by Javadoc and similar tools.
@Inherited: Indicates that an annotation type is automatically inherited.
Ravi Vishwakarma
18-Jul-2024Annotations in Java are a form of metadata that can be added to Java code (classes, methods, variables, etc.). They provide information to the compiler and can be used at runtime by the JVM or other tools for various purposes.
Types of Annotations
Standard Annotations: Java provides several built-in annotations in the
java.lang
package.@Override
: Indicates that a method is overriding a method in a superclass.@Deprecated
: Marks a method or class as deprecated, meaning it should not be used.@SuppressWarnings
: Tells the compiler to suppress specific warnings.Meta-Annotations: Meta-annotations are annotations that apply to other annotations. Some of the meta-annotations are:
@Retention
: Specifies how long the annotation is retained (runtime, class file, source).@Target
: Specifies the kinds of elements an annotation type applies to (methods, fields, classes).@Documented
: Indicates that an annotation should be documented by Javadoc and similar tools.@Inherited
: Indicates that an annotation type is automatically inherited.Example: